import Mysql from "mysql"; //数据库驱动
import fastifyPlugin from "fastify-plugin";
import Exec from "./exec.js"; //执行器封装
import vconf from "../utils/vconf.js";
const config = vconf("config.yml");

// 创建连接池
const { sql } = config;
const pool = Mysql.createPool(sql.mysql);
const mysql = new Exec(pool);

async function fpMysql(fastify, opts, done) {
  fastify.decorate("mysql", mysql);
  //初始化连接池 创建1个测试
  const { code, data, msg } = await mysql.query(
    "SELECT version FROM app_info limit 1;"
  );
  console.log(code === 1 ? "开启连接池..." : "连接池开启失败...");
  done();
}
export default fastifyPlugin(fpMysql);
